{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Check if All the Integers in a Range Are Covered"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #prefix-sum"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #前缀和"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: isCovered"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #检查是否区域内所有整数都被覆盖"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个二维整数数组 <code>ranges</code> 和两个整数 <code>left</code> 和 <code>right</code> 。每个 <code>ranges[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> 表示一个从 <code>start<sub>i</sub></code> 到 <code>end<sub>i</sub></code> 的 <strong>闭区间</strong> 。</p>\n",
    "\n",
    "<p>如果闭区间 <code>[left, right]</code> 内每个整数都被 <code>ranges</code> 中 <strong>至少一个</strong> 区间覆盖，那么请你返回 <code>true</code> ，否则返回 <code>false</code> 。</p>\n",
    "\n",
    "<p>已知区间 <code>ranges[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> ，如果整数 <code>x</code> 满足 <code>start<sub>i</sub> <= x <= end<sub>i</sub></code> ，那么我们称整数<code>x</code> 被覆盖了。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>ranges = [[1,2],[3,4],[5,6]], left = 2, right = 5\n",
    "<b>输出：</b>true\n",
    "<b>解释：</b>2 到 5 的每个整数都被覆盖了：\n",
    "- 2 被第一个区间覆盖。\n",
    "- 3 和 4 被第二个区间覆盖。\n",
    "- 5 被第三个区间覆盖。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>ranges = [[1,10],[10,20]], left = 21, right = 21\n",
    "<b>输出：</b>false\n",
    "<b>解释：</b>21 没有被任何一个区间覆盖。\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 <= ranges.length <= 50</code></li>\n",
    "\t<li><code>1 <= start<sub>i</sub> <= end<sub>i</sub> <= 50</code></li>\n",
    "\t<li><code>1 <= left <= right <= 50</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [check-if-all-the-integers-in-a-range-are-covered](https://leetcode.cn/problems/check-if-all-the-integers-in-a-range-are-covered/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [check-if-all-the-integers-in-a-range-are-covered](https://leetcode.cn/problems/check-if-all-the-integers-in-a-range-are-covered/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[[1,2],[3,4],[5,6]]\\n2\\n5', '[[1,10],[10,20]]\\n21\\n21']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from functools import cmp_to_key\n",
    "\n",
    "\n",
    "def compare_ranges(a: List[int], b: List[int]) -> bool:\n",
    "    if a[0] < b[0] or (a[0] == b[0] and a[1] < b[1]):\n",
    "        return -1\n",
    "    if a[0] == b[0] and a[1] == b[1]:\n",
    "        return 0\n",
    "    return 1\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        ranges.sort(key=cmp_to_key(compare_ranges))\n",
    "        \n",
    "        i: int = 0\n",
    "        while i < len(ranges) and ranges[i][1] < left:\n",
    "            i += 1\n",
    "            \n",
    "        if i == len(ranges) or left < ranges[i][0]:\n",
    "            return False\n",
    "\n",
    "        rightmost: int = ranges[i][1]\n",
    "        i += 1\n",
    "        while i < len(ranges):\n",
    "            if ranges[i][0] - rightmost > 1:\n",
    "                return False\n",
    "            rightmost = max(rightmost, ranges[i][1])\n",
    "            i += 1\n",
    "            \n",
    "        return right <= rightmost"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: list[list[int]], left: int, right: int) -> bool:\n",
    "        sub = [0] * 50\n",
    "        for r in ranges:\n",
    "            for i in range(r[0] - 1, r[1]):\n",
    "                sub[i] = 1\n",
    "        return not 0 in sub[left - 1 : right]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        return all(any(l<=i<=r for l,r in ranges) for i in range(left,right+1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        n = right - left + 1\n",
    "        judge = [False] * n\n",
    "        for r in ranges:\n",
    "            start = r[0]\n",
    "            end = r[1]\n",
    "            if not (end < left or start > right):\n",
    "                for i in range(start-left, end-left+1):\n",
    "                    if i >=0 and i <n:\n",
    "                        judge[i] = True\n",
    "                if all(judge):\n",
    "                    return True\n",
    "        # print(judge)\n",
    "        if not all(judge):\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        diff = defaultdict(int)\n",
    "        for l, r in ranges:\n",
    "            diff[l] += 1\n",
    "            diff[r+1] -= 1\n",
    "        curr = 0\n",
    "        for i in range(1, right + 1):\n",
    "            curr += diff[i]\n",
    "            if curr <= 0 and left <= i:\n",
    "                return False\n",
    "        return True\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        for i in range(left,right + 1):\n",
    "            if all([i not in range(x,y+1) for x,y in ranges]):\n",
    "                return False\n",
    "        else:\n",
    "            return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        cnt = 0\n",
    "        for i in range(left,right + 1):\n",
    "            for x,y in ranges:\n",
    "                if i in range(x,y+1):\n",
    "                    cnt += 1\n",
    "                    break\n",
    "        return cnt == right + 1 - left\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        diff = [0] * 52\n",
    "        s = 0\n",
    "        for l, r in ranges:\n",
    "            diff[l] += 1\n",
    "            diff[r + 1] -= 1\n",
    "        return all(list(accumulate(diff))[left: right + 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        target=set(range(left,right+1))\n",
    "        for l,r in ranges:\n",
    "            for t in range(l,r+1):\n",
    "                if t in target:\n",
    "                    target.remove(t)\n",
    "        return len(target)==0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        list1=[0]*51\n",
    "        for ran in ranges:\n",
    "            for i in range(ran[0],ran[1]+1):\n",
    "                list1[i]=1\n",
    "        for i in range(left,right+1):\n",
    "            if list1[i]==0:\n",
    "                return False\n",
    "        return True\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        cnt = 0\n",
    "        for i in range(left,right + 1):\n",
    "            for x,y in ranges:\n",
    "                if i in range(x,y+1):\n",
    "                    cnt += 1\n",
    "                    break\n",
    "        return cnt == right + 1 - left\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        diff = [0] * 52\n",
    "        for start, end in ranges:\n",
    "            diff[start] += 1\n",
    "            diff[end+1] -= 1\n",
    "\n",
    "        for i in range(1, 52):\n",
    "            diff[i] += diff[i-1]\n",
    "\n",
    "        for i in range(left, right+1):\n",
    "            if diff[i] <= 0:\n",
    "                return False\n",
    "        return True\n",
    "\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        cnt = 0\n",
    "        for i in range(left,right + 1):\n",
    "            for x,y in ranges:\n",
    "                if i in range(x,y+1):\n",
    "                    cnt += 1\n",
    "                    break\n",
    "        return cnt == right + 1 - left\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        ans = [0] * 51\n",
    "        for start, end in ranges:\n",
    "            for i in range(start, end + 1):\n",
    "                ans[i] = 1\n",
    "        for i in range(left, right + 1):\n",
    "            if ans[i] != 1:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        def check(ranges,i):\n",
    "            for ra in ranges:\n",
    "                if i<=ra[1] and i>=ra[0]:\n",
    "                    return True\n",
    "            return False\n",
    "        for i in range(left,right+1):\n",
    "            if not check(ranges,i):\n",
    "                return False\n",
    "        return True\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        ans = True\n",
    "        for i in range(left, right+1):\n",
    "            cur = False\n",
    "            for x, y in ranges:\n",
    "                if x<=i<=y:\n",
    "                    cur = True\n",
    "            ans = ans and cur\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        c=0\n",
    "        for i in range(left,right+1):\n",
    "            for x,y in ranges:\n",
    "                if i in range(x,y+1):\n",
    "                    c+=1\n",
    "                    break\n",
    "        return c==right-left+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        cnt = 0\n",
    "        for i in range(left,right + 1):\n",
    "            for x,y in ranges:\n",
    "                if i in range(x,y+1):\n",
    "                    cnt += 1\n",
    "                    break\n",
    "        return cnt == right + 1 - left\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "\n",
    "        all_nums = []\n",
    "\n",
    "        for i in range(len(ranges)):\n",
    "            for j in range(ranges[i][0], ranges[i][1]+1):\n",
    "                all_nums.append(j)\n",
    "        \n",
    "        for num in range(left, right+1):\n",
    "            if num not in all_nums:\n",
    "                return False\n",
    "        \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        res = []\n",
    "        for i in range(left,right+1):\n",
    "            l = []\n",
    "            for x,y in ranges:\n",
    "                l.append(x<=i<=y)\n",
    "            res.append(any(l))\n",
    "        return all(res)\n",
    "            \n",
    "           \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        diff = [0] * 52   # 差分数组\n",
    "        for l, r in ranges:\n",
    "            diff[l] += 1\n",
    "            diff[r+1] -= 1\n",
    "        # 前缀和\n",
    "        curr = 0\n",
    "        for i in range(1, 51):\n",
    "            curr += diff[i]\n",
    "            if left <= i <= right and curr <= 0:\n",
    "                return False\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        f = [0] * 51\n",
    "        for start, end in ranges:\n",
    "            for i in range(start, end + 1):\n",
    "                f[i] = 1\n",
    "        return all(f[i] for i in range(left, right + 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        cnt = 0\n",
    "        for i in range(left,right + 1):\n",
    "            for x,y in ranges:\n",
    "                if i in range(x,y+1):\n",
    "                    cnt += 1\n",
    "                    break\n",
    "        return cnt == right + 1 - left\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        cnt = 0\n",
    "        for i in range(left,right + 1):\n",
    "            for x,y in ranges:\n",
    "                if i in range(x,y+1):\n",
    "                    cnt += 1\n",
    "                    break\n",
    "        return cnt == right + 1 - left\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        diff = [0] * 52   # 差分数组\n",
    "        for l, r in ranges:\n",
    "            diff[l] += 1\n",
    "            diff[r+1] -= 1\n",
    "        # 前缀和\n",
    "        curr = 0\n",
    "        for i in range(1, 51):\n",
    "            curr += diff[i]\n",
    "            if left <= i <= right and curr <= 0:\n",
    "                return False\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "\n",
    "        all_nums = []\n",
    "\n",
    "        for i in range(len(ranges)):\n",
    "            for j in range(ranges[i][0], ranges[i][1]+1):\n",
    "                all_nums.append(j)\n",
    "        \n",
    "        for num in range(left, right+1):\n",
    "            if num not in all_nums:\n",
    "                return False\n",
    "        \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        for i in range(left,right + 1):\n",
    "            if all([i not in range(x,y+1) for x,y in ranges]):\n",
    "                return False\n",
    "        else:\n",
    "            return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        is_cover=[0]*(right-left+1)\n",
    "        for i,(s,e) in enumerate(ranges):\n",
    "            for j in range(max(s-left,0),min(e-left+1,right-left+1)):\n",
    "                is_cover[j]=1\n",
    "            print(is_cover)\n",
    "        return bool(min(is_cover))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        diff = [0] * 52   # 差分数组\n",
    "        for l, r in ranges:\n",
    "            diff[l] += 1\n",
    "            diff[r+1] -= 1\n",
    "        # 前缀和\n",
    "        curr = 0\n",
    "        for i in range(1, 51):\n",
    "            curr += diff[i]\n",
    "            if left <= i <= right and curr <= 0:\n",
    "                return False\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        diff = [0] * 52\n",
    "        for s, e in ranges:\n",
    "            diff[s] += 1\n",
    "            diff[e + 1] -= 1\n",
    "        return all(v > 0 for i, v in enumerate(accumulate(diff)) if left <= i <= right)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        cnt=0\n",
    "        for i in range(left,right+1):\n",
    "            for j in ranges:\n",
    "                if i>=j[0] and i<=j[1]:\n",
    "                   cnt+=1\n",
    "                   break\n",
    "        return cnt==right-left+1        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        MX = 50\n",
    "        diff = [0] * (MX+2)\n",
    "        for l, r in ranges:\n",
    "            diff[l] += 1\n",
    "            diff[r+1] -= 1\n",
    "        for i in range(MX+1):\n",
    "            diff[i+1] += diff[i]\n",
    "        return all(d > 0 for d in diff[left:right+1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        lst = [0] * 60\n",
    "        for a, b in ranges:\n",
    "            for i in range(a, b + 1):\n",
    "                lst[i] = 1\n",
    "        return all(n == 1 for n in lst[left: right + 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        ranges.sort()\n",
    "        for i, j in ranges:\n",
    "            if i <= left:\n",
    "                if j >= right:\n",
    "                    return True\n",
    "                elif j >= left:\n",
    "                    left = j+1\n",
    "            else:\n",
    "                return False \n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        cnt = 0\n",
    "        for i in range(left,right + 1):\n",
    "            for x,y in ranges:\n",
    "                if i in range(x,y+1):\n",
    "                    cnt += 1\n",
    "                    break\n",
    "        return cnt == right + 1 - left"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        target=set(range(left,right+1))\n",
    "        for i,n in ranges:\n",
    "            for m in range(i,n+1):\n",
    "                if m in target:\n",
    "                    target.remove(m)\n",
    "        return len(target)==0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        res = []\n",
    "        for i in range(left, right+1):\n",
    "            ans = []\n",
    "            for l,r in ranges:\n",
    "                ans.append(l <= i <= r)\n",
    "            res.append(any(ans))\n",
    "        return all(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        s = [0] * 51\n",
    "        for a, b in ranges:\n",
    "            for i in range(a, b + 1):\n",
    "                s[i] = 1\n",
    "        for i in range(left, right + 1):\n",
    "            if not s[i]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool: \n",
    "        # 区间排序  \n",
    "        ranges.sort(key = lambda x: x[0])\n",
    "        for x in ranges:\n",
    "            if left < x[0]:\n",
    "                # 如果left 小于当前区间的开始数，说明无法覆盖[left, right]区间的所有整数\n",
    "                return False\n",
    "            if x[0] <= left <= x[1]: # 如果left 在某个区间中\n",
    "                # 移除 该区间覆盖的整数\n",
    "                left = x[1] + 1\n",
    "        #  如果left 超过right,说明说有整数都被覆盖了\n",
    "        return left > right\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "class Solution:\n",
    "    def isCovered(self, ranges, left, right):\n",
    "        dict1 = defaultdict(bool)\n",
    "        \n",
    "        for s, e in ranges:\n",
    "            for i in range(s, e + 1):\n",
    "                dict1[i] = True\n",
    "        \n",
    "        for i in range(left, right + 1):\n",
    "            if dict1[i] == False:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        diff = [0] * 52   # 差分数组\n",
    "        for l, r in ranges:\n",
    "            diff[l] += 1\n",
    "            diff[r+1] -= 1\n",
    "        # 前缀和\n",
    "        for i, n in enumerate(accumulate(diff)):\n",
    "            if left <= i <= right and n <= 0:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        cnt = 0\n",
    "        for i in range(left,right + 1):\n",
    "            for x,y in ranges:\n",
    "                if i in range(x,y+1):\n",
    "                    cnt += 1\n",
    "                    break\n",
    "        return cnt == right + 1 - left"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        return not (set(range(left, right + 1)) - {i for x in ranges for i in range(x[0], x[1] + 1)})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        # 暴力\n",
    "        s = set(range(left, right+1))\n",
    "        for x, y in ranges:\n",
    "            s.difference_update(range(x, y+1))\n",
    "            if not s:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        lst = [0] * 60\n",
    "        for a, b in ranges:\n",
    "            lst[a] += 1\n",
    "            lst[b + 1] -= 1\n",
    "        ans = [lst[0]]\n",
    "        for i in range(1, len(lst)):\n",
    "            ans.append(lst[i] + ans[i - 1])\n",
    "        return all(n != 0 for n in ans[left: right + 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        diff = [0]*52\n",
    "        for l,r in ranges:\n",
    "            diff[l] +=1\n",
    "            diff[r+1] -=1\n",
    "        curr=0\n",
    "        for i in range(1,51):\n",
    "            curr += diff[i]\n",
    "            if left<=i<=right and curr <= 0:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        r_list = []\n",
    "        for rl in ranges:\n",
    "            r_list = r_list + list(range(rl[0],rl[1]+1))\n",
    "        for i in range(left,right+1):\n",
    "            if i not in r_list:\n",
    "                return False\n",
    "        return True\n",
    "\n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        return all(any(l <= i <= r for l, r in ranges) for i in range(left, right + 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        cnt = 0\n",
    "        for i in range(left,right + 1):\n",
    "            for x,y in ranges:\n",
    "                if i in range(x,y+1):\n",
    "                    cnt += 1\n",
    "                    break\n",
    "        return cnt == right + 1 - left\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        diff = defaultdict(int)\n",
    "        for l,r in ranges:\n",
    "            diff[l]+=1\n",
    "            diff[r+1]-=1\n",
    "        curr =0\n",
    "        for i in range(1,right+1):\n",
    "            curr +=diff[i]\n",
    "            if curr<=0 and left<=i:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        tmp = set()\n",
    "        for i in ranges:\n",
    "            for j in range(i[0],i[1] + 1):\n",
    "                tmp.add(j)\n",
    "        return all(x in tmp for x in range(left, right + 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        res = []\n",
    "        for i in range(left,right+1):\n",
    "            curr = []\n",
    "            for x,y in ranges:\n",
    "                curr.append(x<=i<=y)\n",
    "            res.append(any(curr))\n",
    "        return all(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        # 暴力\n",
    "        l = set()\n",
    "        for x, y in ranges:\n",
    "            l.update(range(x, y+1))\n",
    "        return l >= set(range(left, right+1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        s=set()\n",
    "        for i in ranges:\n",
    "            s=s|set(j for j in range(i[0],i[1]+1))\n",
    "        return set(i for i in range(left,right+1)) <= s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        lst = []\n",
    "        for i in ranges:\n",
    "            for k in range(i[0], i[1]+1):\n",
    "                if k not in lst:\n",
    "                    lst.append(k)\n",
    "        for i in range(left, right+1):\n",
    "            if i not in lst:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        diff = [0] * 52   # 差分数组\n",
    "        for l, r in ranges:\n",
    "            diff[l] += 1\n",
    "            diff[r+1] -= 1\n",
    "        # 前缀和\n",
    "        curr = 0\n",
    "        for i in range(1, 51):\n",
    "            curr += diff[i]\n",
    "            if left <= i <= right and curr <= 0:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        temp_list = []\n",
    "        for i in ranges:\n",
    "            for j in range(i[0], i[1] + 1):\n",
    "                temp_list.append(j)\n",
    "        for k in range(left, right + 1):\n",
    "            if k not in temp_list:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        d=[0]*(52)\n",
    "        for i in range(len(ranges)):\n",
    "            d[ranges[i][0]]+=1\n",
    "            d[ranges[i][1]+1]-=1\n",
    "        cur=0\n",
    "        for i in range(right+1):\n",
    "            cur+=d[i]\n",
    "            if left<=i<=right and cur<=0:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        cnt = 0\n",
    "        for i in range(left,right + 1):\n",
    "            for x,y in ranges:\n",
    "                if i in range(x,y+1):\n",
    "                    cnt += 1\n",
    "                    break\n",
    "        return cnt == right + 1 - left\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        res = [0]*51\n",
    "        for x,y in ranges:\n",
    "            for i in range(x,y+1):\n",
    "                res[i] = 1\n",
    "        for i in range(left,right+1):\n",
    "            if res[i] != 1:\n",
    "                return False\n",
    "        return True\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "\n",
    "        all_nums = []\n",
    "\n",
    "        for i in range(len(ranges)):\n",
    "            for j in range(ranges[i][0], ranges[i][1]+1):\n",
    "                all_nums.append(j)\n",
    "        \n",
    "        for num in range(left, right+1):\n",
    "            if num not in all_nums:\n",
    "                return False\n",
    "        \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        print(ranges)\n",
    "        \n",
    "        for i in range(left,right+1):\n",
    "            flag=0\n",
    "            for interval in ranges:\n",
    "                if interval[0]<=i<=interval[1]:\n",
    "                    flag=1\n",
    "                    break\n",
    "            if flag==0:\n",
    "                return False\n",
    "        return True\n",
    "\n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        ad={}\n",
    "        for i in ranges:\n",
    "            for j in range(i[0],i[1]+1):\n",
    "                ad[j]=ad.get(j,0)+1\n",
    "        flag=True\n",
    "        for i in range(left,right+1):\n",
    "            if i not in ad:\n",
    "                flag=False\n",
    "        return flag\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        r = [False] * (right-left+1)\n",
    "        for (start, end) in ranges:\n",
    "            for i in range(start, end+1):\n",
    "                if i >= left and i <= right:\n",
    "                    r[i-left] = True\n",
    "        return all(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        # 初始化一个长度为51的数组，初始值都是False\n",
    "        covered = [False] * 51\n",
    "        \n",
    "        # 遍历每一个范围，将其标记为覆盖\n",
    "        for start, end in ranges:\n",
    "            for i in range(start, end + 1):\n",
    "                covered[i] = True\n",
    "        \n",
    "        # 检查从left到right的每一个数字是否都被覆盖\n",
    "        for i in range(left, right + 1):\n",
    "            if not covered[i]:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        temp = list(range(left, right + 1))\n",
    "        totalList = []\n",
    "        for i in ranges:\n",
    "            totalList = totalList + list(range(i[0], i[1] + 1))\n",
    "\n",
    "        intersection = list(set(temp) & set(totalList))\n",
    "        intersection.sort()\n",
    "        if temp == intersection:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:   \n",
    "        ranges.sort(key = lambda x: x[0])\n",
    "        for x in ranges:\n",
    "            if left < x[0]:\n",
    "                return False\n",
    "            if x[0] <= left <= x[1]:\n",
    "                left = x[1] + 1\n",
    "        return left > right\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        mat = [0 for _ in range(51)]\n",
    "        for item in ranges:\n",
    "            for i in range(item[0],item[1]+1):\n",
    "                mat[i] += 1\n",
    "            \n",
    "        for i in range(left,right+1):\n",
    "            if mat[i] == 0:\n",
    "                return False\n",
    "        return True\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        diff = [0] * 52   # 差分数组\n",
    "        for l, r in ranges:\n",
    "            diff[l] += 1\n",
    "            diff[r+1] -= 1\n",
    "        # 前缀和\n",
    "        curr = 0\n",
    "        for i in range(1, 51):\n",
    "            curr += diff[i]\n",
    "            if left <= i <= right and curr <= 0:\n",
    "                return False\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        ranges.sort()\n",
    "        for x, y in ranges:\n",
    "            if x > left:\n",
    "                return False\n",
    "            left = max(left, y+1)\n",
    "            if y >= right:\n",
    "                return True\n",
    "        return False\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        diff = defaultdict(int)\n",
    "        for l, r in ranges:\n",
    "            diff[l] += 1\n",
    "            diff[r+1] -= 1\n",
    "        curr = 0\n",
    "        for i in range(1, right + 1):\n",
    "            curr += diff[i]\n",
    "            if curr <= 0 and left <= i:\n",
    "                return False\n",
    "        return True\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        ranges.sort(key=lambda x: x[0])\n",
    "        new_ranges = [ranges[0]]\n",
    "        for r in ranges[1:]:\n",
    "            if r[0] <= new_ranges[-1][1] + 1:\n",
    "                new_ranges[-1][1] = max(r[1], new_ranges[-1][1])\n",
    "            else:\n",
    "                new_ranges.append(r)\n",
    "        # 判断left和right是否在new_ranges的某个区间内\n",
    "        print(new_ranges)\n",
    "        for r in new_ranges:\n",
    "            if left >= r[0] and right <= r[1]:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        diff = [0] * 55 \n",
    "        for l,r in ranges :\n",
    "            diff[l] += 1 \n",
    "            diff[r+1] -= 1 \n",
    "        cur = 0\n",
    "        for i in range(1,55) :\n",
    "            cur += diff[i] \n",
    "            if i >= left and i <= right and cur <= 0 :\n",
    "                return False \n",
    "        return True "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        covered = [0] * 51\n",
    "        for start, end in ranges:\n",
    "            for i in range(start, end + 1):\n",
    "                covered[i] = 1\n",
    "        for i in range(left, right + 1):\n",
    "            if covered[i] != 1:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        res = []\n",
    "        for i in range(left,right+1):\n",
    "            curr = []\n",
    "            for x,y in ranges:\n",
    "                curr.append(x<=i<=y)\n",
    "            res.append(any(curr))\n",
    "        return all(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        covers = set()\n",
    "        for l, r in ranges:\n",
    "            covers.update({i for i in range(l, r + 1)})\n",
    "        return all(i in covers for i in range(left, right + 1))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:\n",
    "        ranges.sort()\n",
    "        for i in range(left, right+1):\n",
    "            for j in ranges:\n",
    "                if i < j[0]:\n",
    "                    return False\n",
    "                if i <= j[1]:\n",
    "                    break\n",
    "                else:\n",
    "                    continue\n",
    "            else:\n",
    "                return False\n",
    "        return True\n",
    "                \n",
    "            \n",
    "\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
